package org.jeecg.modules.demo.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.modules.estar.tw.util.StringUtils;
import org.jeecg.modules.inventory.entity.BussInventoryPurchaseOrder;
import org.jeecg.modules.inventory.service.IBussInventoryPurchaseOrderService;
import org.jeecg.modules.inventory.vo.BussInventoryPurchaseOrderVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.entity.BussInventoryInoutDetails;
import org.jeecg.modules.inventory.entity.BussInventory;
import org.jeecg.modules.inventory.vo.BussInventoryPage;
import org.jeecg.modules.inventory.service.IBussInventoryService;
import org.jeecg.modules.inventory.service.IBussInventoryDetailsService;
import org.jeecg.modules.inventory.service.IBussInventoryInoutDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 库存管理
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Api(tags="库存管理")
@RestController
@RequestMapping("/inventory/bussInventory")
@Slf4j
public class BussInventoryController {
	@Autowired
	private IBussInventoryService bussInventoryService;
	@Autowired
	private IBussInventoryDetailsService bussInventoryDetailsService;
	@Autowired
	private IBussInventoryInoutDetailsService bussInventoryInoutDetailsService;
	@Autowired
	private IBussInventoryPurchaseOrderService bussInventoryPurchaseOrderService;

	/**
	 * 分页列表查询
	 *
	 * @param bussInventory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库存管理-分页列表查询")
	@ApiOperation(value="库存管理-分页列表查询", notes="库存管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BussInventory>> queryPageList(BussInventory bussInventory,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		IPage<BussInventory> pageList = new Page<>();
		//如果没选仓库直接返回空
		if(StringUtils.isEmpty(bussInventory.getWarehouseId())){
			return Result.OK(pageList);
		}
		QueryWrapper<BussInventory> queryWrapper = QueryGenerator.initQueryWrapper(bussInventory, req.getParameterMap());
		Page<BussInventory> page = new Page<BussInventory>(pageNo, pageSize);
		pageList = bussInventoryService.myPage(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param bussInventoryPage
	 * @return
	 */
	@AutoLog(value = "库存管理-添加")
	@ApiOperation(value="库存管理-添加", notes="库存管理-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:buss_inventory:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BussInventoryPage bussInventoryPage) {
		BussInventory bussInventory = new BussInventory();
		BeanUtils.copyProperties(bussInventoryPage, bussInventory);
		bussInventoryService.saveMain(bussInventory, bussInventoryPage.getBussInventoryDetailsList(),bussInventoryPage.getBussInventoryInoutDetailsList());
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param bussInventoryPage
	 * @return
	 */
	@AutoLog(value = "库存管理-编辑")
	@ApiOperation(value="库存管理-编辑", notes="库存管理-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:buss_inventory:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BussInventoryPage bussInventoryPage) {
		BussInventory bussInventory = new BussInventory();
		BeanUtils.copyProperties(bussInventoryPage, bussInventory);
		BussInventory bussInventoryEntity = bussInventoryService.getById(bussInventory.getId());
		if(bussInventoryEntity==null) {
			return Result.error("未找到对应数据");
		}
		bussInventoryService.updateMain(bussInventory, bussInventoryPage.getBussInventoryDetailsList(),bussInventoryPage.getBussInventoryInoutDetailsList());
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存管理-通过id删除")
	@ApiOperation(value="库存管理-通过id删除", notes="库存管理-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:buss_inventory:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		bussInventoryService.delMain(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存管理-批量删除")
	@ApiOperation(value="库存管理-批量删除", notes="库存管理-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:buss_inventory:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bussInventoryService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "库存管理-通过id查询")
	@ApiOperation(value="库存管理-通过id查询", notes="库存管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BussInventory> queryById(@RequestParam(name="id",required=true) String id) {
		BussInventory bussInventory = bussInventoryService.getById(id);
		if(bussInventory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bussInventory);

	}

	@ApiOperation(value="库存管理-通过仓库跟物料查询", notes="库存管理-通过仓库跟物料查询")
	@PostMapping(value = "/queryByIds")
	public Result<List<BussInventoryPage>> queryByIds(@RequestBody Map<String, Object> map) {
		List<String> ids = (List<String>) map.get("ids");
		String warehouseId = map.get("warehouseId").toString();
		List<BussInventoryPage> bussInventorys = bussInventoryService.getByIds(ids,warehouseId);
		if (bussInventorys == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bussInventorys);

	}

	/**
	 * 通过id查询
	 *
	 * @param warehouseId
	 * @param materialId
	 * @return
	 */
	//@AutoLog(value = "库存明细通过主表ID查询")
	@ApiOperation(value="库存明细主表ID查询", notes="库存明细-通主表ID查询")
	@GetMapping(value = "/queryBussInventoryDetailsByMainId")
	public Result<List<BussInventoryDetails>> queryBussInventoryDetailsListByMainId(@RequestParam(name="warehouseId",required=true) String warehouseId,@RequestParam(name="materialId",required=true) String materialId) {
		List<BussInventoryDetails> bussInventoryDetailsList = bussInventoryDetailsService.selectByMainId(warehouseId,materialId);
		return Result.OK(bussInventoryDetailsList);
	}
	/**
	 * 通过id查询
	 *
	 * @param warehouseId
	 * @param materialId
	 * @return
	 */
	//@AutoLog(value = "库存出入库明细通过主表ID查询")
	@ApiOperation(value="库存出入库明细主表ID查询", notes="库存出入库明细-通主表ID查询")
	@GetMapping(value = "/queryBussInventoryInoutDetailsByMainId")
	public Result<List<BussInventoryInoutDetails>> queryBussInventoryInoutDetailsListByMainId(@RequestParam(name="warehouseId",required=true) String warehouseId,@RequestParam(name="materialId",required=true) String materialId) {
		List<BussInventoryInoutDetails> bussInventoryInoutDetailsList = bussInventoryInoutDetailsService.selectByMainId(warehouseId,materialId);
		return Result.OK(bussInventoryInoutDetailsList);
	}

	@ApiOperation(value="库存出入库明细库存明细查询", notes="库存出入库明细-通库存明细ID查询")
	@GetMapping(value = "/queryBussInventoryInoutDetailsByDetailId")
	public Result<IPage<BussInventoryInoutDetails>> queryBussInventoryInoutDetailsByDetailId(@RequestParam(name="id",required=true) String id) {
		Page<BussInventoryInoutDetails> page = new Page<BussInventoryInoutDetails>(1, 100);
		IPage<BussInventoryInoutDetails> bussInventoryInoutDetailsList = bussInventoryInoutDetailsService.selectByDetailId(page,id);
		return Result.OK(bussInventoryInoutDetailsList);
	}

	/**
	 * 通过id查询
	 *
	 * @param warehouseId
	 * @param materialId
	 * @return
	 */
	//@AutoLog(value = "库存关联采购订单通过主表ID查询")
	@ApiOperation(value="库存关联采购订单主表ID查询", notes="库存关联采购订单-通主表ID查询")
	@GetMapping(value = "/queryBussInventoryPurchaseOrderByMainId")
	public Result<List<BussInventoryPurchaseOrderVo>> queryBussInventoryPurchaseOrderListByMainId(@RequestParam(name="warehouseId",required=true) String warehouseId,@RequestParam(name="materialId",required=true) String materialId) {
		List<BussInventoryPurchaseOrderVo> bussInventoryPurchaseOrderList = bussInventoryPurchaseOrderService.selectByMainId(warehouseId,materialId);
		return Result.OK(bussInventoryPurchaseOrderList);
	}

	@ApiOperation(value="库存管理-出库列表查询", notes="库存管理-出库列表查询")
	@GetMapping(value = "/inventoryList")
	public Result<IPage<BussInventory>> queryInventoryList(BussInventory bussInventory,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		IPage<BussInventory> pageList = new Page<>();
		//如果没选仓库直接返回空
		if(StringUtils.isEmpty(bussInventory.getWarehouseId())){
			return Result.OK(pageList);
		}
		QueryWrapper<BussInventory> queryWrapper = QueryGenerator.initQueryWrapper(bussInventory, req.getParameterMap());
		Page<BussInventory> page = new Page<BussInventory>(pageNo, pageSize);
		pageList = bussInventoryService.myInventoryPage(page, bussInventory);
		return Result.OK(pageList);
	}
}
